home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / DataInputStream.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  18.5 KB  |  565 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)DataInputStream.java    1.46 98/06/29
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * A data input stream lets an application read primitive Java data
  19.  * types from an underlying input stream in a machine-independent
  20.  * way. An application uses a data output stream to write data that
  21.  * can later be read by a data input stream.
  22.  * <p>
  23.  * Data input streams and data output streams represent Unicode
  24.  * strings in a format that is a slight modification of UTF-8. (For
  25.  * more information, see X/Open Company Ltd., "File System Safe
  26.  * UCS Transformation Format (FSS_UTF)", X/Open Preliminary
  27.  * Specification, Document Number: P316. This information also
  28.  * appears in ISO/IEC 10646, Annex P.)
  29.  * <p>
  30.  * All characters in the range <code>'\u0001'</code> to
  31.  * <code>'\u007F'</code> are represented by a single byte:
  32.  * <center><table border="3">
  33.  *   <tr><td><i>0</i></td>  <td>bits 0-7</td></tr>
  34.  * </table></center>
  35.  * <p>
  36.  * The null character <code>'\u0000'</code> and characters in the
  37.  * range <code>'\u0080'</code> to <code>'\u07FF'</code> are
  38.  * represented by a pair of bytes:
  39.  * <center><table border="3">
  40.  *   <tr><td>1</td>  <td>1</td>  <td>0</td>  <td>bits 6-10</td></tr>
  41.  *   <tr><td>1</td>  <td>0</td>  <td colspan=2>bits 0-5</td></tr>
  42.  * </table></center><br>
  43.  * Characters in the range <code>'\u0800'</code> to
  44.  * <code>'\uFFFF'</code> are represented by three bytes:
  45.  * <center><table border="3">
  46.  *   <tr><td>1</td>  <td>1</td>  <td>1</td>  <td>0</td>  <td>bits 12-15</td</tr>
  47.  *   <tr><td>1</td>  <td>0</td>  <td colspan=3>bits 6-11</td></tr>
  48.  *   <tr><td>1</td>  <td>0</td>  <td colspan=3>bits 0-5</td></tr>
  49.  * </table></center>
  50.  * <p>
  51.  * The two differences between this format and the
  52.  * "standard" UTF-8 format are the following:
  53.  * <ul>
  54.  * <li>The null byte <code>'\u0000'</code> is encoded in 2-byte format
  55.  *     rather than 1-byte, so that the encoded strings never have
  56.  *     embedded nulls.
  57.  * <li>Only the 1-byte, 2-byte, and 3-byte formats are used.
  58.  * </ul>
  59.  *
  60.  * @author  Arthur van Hoff
  61.  * @version 1.46, 06/29/98
  62.  * @see     java.io.DataOutputStream
  63.  * @since   JDK1.0
  64.  */
  65. public
  66. class DataInputStream extends FilterInputStream implements DataInput {
  67.     /**
  68.      * Creates a <code>FilterInputStream</code>
  69.      * and saves its  argument, the input stream
  70.      * <code>in</code>, for later use. An internal
  71.      *
  72.      * @param  in   the input stream.
  73.      */
  74.     public DataInputStream(InputStream in) {
  75.     super(in);
  76.     }
  77.  
  78.     /**
  79.      * See the general contract of the <code>read</code>
  80.      * method of <code>DataInput</code>.
  81.      * <p>
  82.      * Bytes
  83.      * for this operation are read from the contained
  84.      * input stream.
  85.      *
  86.      * @param      b   the buffer into which the data is read.
  87.      * @return     the total number of bytes read into the buffer, or
  88.      *             <code>-1</code> if there is no more data because the end
  89.      *             of the stream has been reached.
  90.      * @exception  IOException  if an I/O error occurs.
  91.      * @see        java.io.FilterInputStream#in
  92.      * @see        java.io.InputStream#read(byte[], int, int)
  93.      */
  94.     public final int read(byte b[]) throws IOException {
  95.     return in.read(b, 0, b.length);
  96.     }
  97.  
  98.     /**
  99.      * See the general contract of the <code>read</code>
  100.      * method of <code>DataInput</code>.
  101.      * <p>
  102.      * Bytes
  103.      * for this operation are read from the contained
  104.      * input stream.
  105.      *
  106.      * @param      b     the buffer into which the data is read.
  107.      * @param      off   the start offset of the data.
  108.      * @param      len   the maximum number of bytes read.
  109.      * @return     the total number of bytes read into the buffer, or
  110.      *             <code>-1</code> if there is no more data because the end
  111.      *             of the stream has been reached.
  112.      * @exception  IOException  if an I/O error occurs.
  113.      * @see        java.io.FilterInputStream#in
  114.      * @see        java.io.InputStream#read(byte[], int, int)
  115.      */
  116.     public final int read(byte b[], int off, int len) throws IOException {
  117.     return in.read(b, off, len);
  118.     }
  119.  
  120.     /**
  121.      * See the general contract of the <code>readFully</code>
  122.      * method of <code>DataInput</code>.
  123.      * <p>
  124.      * Bytes
  125.      * for this operation are read from the contained
  126.      * input stream.
  127.      *
  128.      * @param      b   the buffer into which the data is read.
  129.      * @exception  EOFException  if this input stream reaches the end before
  130.      *               reading all the bytes.
  131.      * @exception  IOException   if an I/O error occurs.
  132.      * @see        java.io.FilterInputStream#in
  133.      */
  134.     public final void readFully(byte b[]) throws IOException {
  135.     readFully(b, 0, b.length);
  136.     }
  137.  
  138.     /**
  139.      * See the general contract of the <code>readFully</code>
  140.      * method of <code>DataInput</code>.
  141.      * <p>
  142.      * Bytes
  143.      * for this operation are read from the contained
  144.      * input stream.
  145.      *
  146.      * @param      b     the buffer into which the data is read.
  147.      * @param      off   the start offset of the data.
  148.      * @param      len   the number of bytes to read.
  149.      * @exception  EOFException  if this input stream reaches the end before
  150.      *               reading all the bytes.
  151.      * @exception  IOException   if an I/O error occurs.
  152.      * @see        java.io.FilterInputStream#in
  153.      */
  154.     public final void readFully(byte b[], int off, int len) throws IOException {
  155.     InputStream in = this.in;
  156.     int n = 0;
  157.     while (n < len) {
  158.         int count = in.read(b, off + n, len - n);
  159.         if (count < 0)
  160.         throw new EOFException();
  161.         n += count;
  162.     }
  163.     }
  164.  
  165.     /**
  166.      * See the general contract of the <code>skipBytes</code>
  167.      * method of <code>DataInput</code>.
  168.      * <p>
  169.      * Bytes
  170.      * for this operation are read from the contained
  171.      * input stream.
  172.      *
  173.      * @param      n   the number of bytes to be skipped.
  174.      * @return     the actual number of bytes skipped.
  175.      * @exception  IOException   if an I/O error occurs.
  176.      */
  177.     public final int skipBytes(int n) throws IOException {
  178.     InputStream in = this.in;
  179.     int total = 0;
  180.     int cur = 0;
  181.  
  182.     while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
  183.         total += cur;
  184.     }
  185.  
  186.     return total;
  187.     }
  188.  
  189.     /**
  190.      * See the general contract of the <code>readBoolean</code>
  191.      * method of <code>DataInput</code>.
  192.      * <p>
  193.      * Bytes
  194.      * for this operation are read from the contained
  195.      * input stream.
  196.      *
  197.      * @return     the <code>boolean</code> value read.
  198.      * @exception  EOFException  if this input stream has reached the end.
  199.      * @exception  IOException   if an I/O error occurs.
  200.      * @see        java.io.FilterInputStream#in
  201.      */
  202.     public final boolean readBoolean() throws IOException {
  203.     int ch = in.read();
  204.     if (ch < 0)
  205.         throw new EOFException();
  206.     return (ch != 0);
  207.     }
  208.  
  209.     /**
  210.      * See the general contract of the <code>readByte</code>
  211.      * method of <code>DataInput</code>.
  212.      * <p>
  213.      * Bytes
  214.      * for this operation are read from the contained
  215.      * input stream.
  216.      *
  217.      * @return     the next byte of this input stream as a signed 8-bit
  218.      *             <code>byte</code>.
  219.      * @exception  EOFException  if this input stream has reached the end.
  220.      * @exception  IOException   if an I/O error occurs.
  221.      * @see        java.io.FilterInputStream#in
  222.      */
  223.     public final byte readByte() throws IOException {
  224.     int ch = in.read();
  225.     if (ch < 0)
  226.         throw new EOFException();
  227.     return (byte)(ch);
  228.     }
  229.  
  230.     /**
  231.      * See the general contract of the <code>readUnsignedByte</code>
  232.      * method of <code>DataInput</code>.
  233.      * <p>
  234.      * Bytes
  235.      * for this operation are read from the contained
  236.      * input stream.
  237.      *
  238.      * @return     the next byte of this input stream, interpreted as an
  239.      *             unsigned 8-bit number.
  240.      * @exception  EOFException  if this input stream has reached the end.
  241.      * @exception  IOException   if an I/O error occurs.
  242.      * @see         java.io.FilterInputStream#in
  243.      */
  244.     public final int readUnsignedByte() throws IOException {
  245.     int ch = in.read();
  246.     if (ch < 0)
  247.         throw new EOFException();
  248.     return ch;
  249.     }
  250.  
  251.     /**
  252.      * See the general contract of the <code>readShort</code>
  253.      * method of <code>DataInput</code>.
  254.      * <p>
  255.      * Bytes
  256.      * for this operation are read from the contained
  257.      * input stream.
  258.      *
  259.      * @return     the next two bytes of this input stream, interpreted as a
  260.      *             signed 16-bit number.
  261.      * @exception  EOFException  if this input stream reaches the end before
  262.      *               reading two bytes.
  263.      * @exception  IOException   if an I/O error occurs.
  264.      * @see        java.io.FilterInputStream#in
  265.      */
  266.     public final short readShort() throws IOException {
  267.     InputStream in = this.in;
  268.     int ch1 = in.read();
  269.     int ch2 = in.read();
  270.     if ((ch1 | ch2) < 0)
  271.          throw new EOFException();
  272.     return (short)((ch1 << 8) + (ch2 << 0));
  273.     }
  274.  
  275.     /**
  276.      * See the general contract of the <code>readUnsignedShort</code>
  277.      * method of <code>DataInput</code>.
  278.      * <p>
  279.      * Bytes
  280.      * for this operation are read from the contained
  281.      * input stream.
  282.      *
  283.      * @return     the next two bytes of this input stream, interpreted as an
  284.      *             unsigned 16-bit integer.
  285.      * @exception  EOFException  if this input stream reaches the end before
  286.      *               reading two bytes.
  287.      * @exception  IOException   if an I/O error occurs.
  288.      * @see        java.io.FilterInputStream#in
  289.      */
  290.     public final int readUnsignedShort() throws IOException {
  291.     InputStream in = this.in;
  292.     int ch1 = in.read();
  293.     int ch2 = in.read();
  294.     if ((ch1 | ch2) < 0)
  295.          throw new EOFException();
  296.     return (ch1 << 8) + (ch2 << 0);
  297.     }
  298.  
  299.     /**
  300.      * See the general contract of the <code>readChar</code>
  301.      * method of <code>DataInput</code>.
  302.      * <p>
  303.      * Bytes
  304.      * for this operation are read from the contained
  305.      * input stream.
  306.      *
  307.      * @return     the next two bytes of this input stream as a Unicode
  308.      *             character.
  309.      * @exception  EOFException  if this input stream reaches the end before
  310.      *               reading two bytes.
  311.      * @exception  IOException   if an I/O error occurs.
  312.      * @see        java.io.FilterInputStream#in
  313.      */
  314.     public final char readChar() throws IOException {
  315.     InputStream in = this.in;
  316.     int ch1 = in.read();
  317.     int ch2 = in.read();
  318.     if ((ch1 | ch2) < 0)
  319.          throw new EOFException();
  320.     return (char)((ch1 << 8) + (ch2 << 0));
  321.     }
  322.  
  323.     /**
  324.      * See the general contract of the <code>readInt</code>
  325.      * method of <code>DataInput</code>.
  326.      * <p>
  327.      * Bytes
  328.      * for this operation are read from the contained
  329.      * input stream.
  330.      *
  331.      * @return     the next four bytes of this input stream, interpreted as an
  332.      *             <code>int</code>.
  333.      * @exception  EOFException  if this input stream reaches the end before
  334.      *               reading four bytes.
  335.      * @exception  IOException   if an I/O error occurs.
  336.      * @see        java.io.FilterInputStream#in
  337.      */
  338.     public final int readInt() throws IOException {
  339.     InputStream in = this.in;
  340.     int ch1 = in.read();
  341.     int ch2 = in.read();
  342.     int ch3 = in.read();
  343.     int ch4 = in.read();
  344.     if ((ch1 | ch2 | ch3 | ch4) < 0)
  345.          throw new EOFException();
  346.     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
  347.     }
  348.  
  349.     /**
  350.      * See the general contract of the <code>readLong</code>
  351.      * method of <code>DataInput</code>.
  352.      * <p>
  353.      * Bytes
  354.      * for this operation are read from the contained
  355.      * input stream.
  356.      *
  357.      * @return     the next eight bytes of this input stream, interpreted as a
  358.      *             <code>long</code>.
  359.      * @exception  EOFException  if this input stream reaches the end before
  360.      *               reading eight bytes.
  361.      * @exception  IOException   if an I/O error occurs.
  362.      * @see        java.io.FilterInputStream#in
  363.      */
  364.     public final long readLong() throws IOException {
  365.     InputStream in = this.in;
  366.     return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
  367.     }
  368.  
  369.     /**
  370.      * See the general contract of the <code>readFloat</code>
  371.      * method of <code>DataInput</code>.
  372.      * <p>
  373.      * Bytes
  374.      * for this operation are read from the contained
  375.      * input stream.
  376.      *
  377.      * @return     the next four bytes of this input stream, interpreted as a
  378.      *             <code>float</code>.
  379.      * @exception  EOFException  if this input stream reaches the end before
  380.      *               reading four bytes.
  381.      * @exception  IOException   if an I/O error occurs.
  382.      * @see        java.io.DataInputStream#readInt()
  383.      * @see        java.lang.Float#intBitsToFloat(int)
  384.      */
  385.     public final float readFloat() throws IOException {
  386.     return Float.intBitsToFloat(readInt());
  387.     }
  388.  
  389.     /**
  390.      * See the general contract of the <code>readDouble</code>
  391.      * method of <code>DataInput</code>.
  392.      * <p>
  393.      * Bytes
  394.      * for this operation are read from the contained
  395.      * input stream.
  396.      *
  397.      * @return     the next eight bytes of this input stream, interpreted as a
  398.      *             <code>double</code>.
  399.      * @exception  EOFException  if this input stream reaches the end before
  400.      *               reading eight bytes.
  401.      * @exception  IOException   if an I/O error occurs.
  402.      * @see        java.io.DataInputStream#readLong()
  403.      * @see        java.lang.Double#longBitsToDouble(long)
  404.      */
  405.     public final double readDouble() throws IOException {
  406.     return Double.longBitsToDouble(readLong());
  407.     }
  408.  
  409.     private char lineBuffer[];
  410.  
  411.     /**
  412.      * See the general contract of the <code>readLine</code>
  413.      * method of <code>DataInput</code>.
  414.      * <p>
  415.      * Bytes
  416.      * for this operation are read from the contained
  417.      * input stream.
  418.      *
  419.      * @deprecated This method does not properly convert bytes to characters.
  420.      * As of JDK 1.1, the preferred way to read lines of text is via the
  421.      * <code>BufferedReader.readLine()</code> method.  Programs that use the
  422.      * <code>DataInputStream</code> class to read lines can be converted to use
  423.      * the <code>BufferedReader</code> class by replacing code of the form:
  424.      * <blockquote><pre>
  425.      *     DataInputStream d = new DataInputStream(in);
  426.      * </pre></blockquote>
  427.      * with:
  428.      * <blockquote><pre>
  429.      *     BufferedReader d
  430.      *          = new BufferedReader(new InputStreamReader(in));
  431.      * </pre></blockquote>
  432.      *
  433.      * @return     the next line of text from this input stream.
  434.      * @exception  IOException  if an I/O error occurs.
  435.      * @see        java.io.BufferedReader#readLine()
  436.      * @see        java.io.FilterInputStream#in
  437.      */
  438.     public final String readLine() throws IOException {
  439.     InputStream in = this.in;
  440.     char buf[] = lineBuffer;
  441.  
  442.     if (buf == null) {
  443.         buf = lineBuffer = new char[128];
  444.     }
  445.  
  446.     int room = buf.length;
  447.     int offset = 0;
  448.     int c;
  449.  
  450. loop:    while (true) {
  451.         switch (c = in.read()) {
  452.           case -1:
  453.           case '\n':
  454.         break loop;
  455.  
  456.           case '\r':
  457.         int c2 = in.read();
  458.         if ((c2 != '\n') && (c2 != -1)) {
  459.             if (!(in instanceof PushbackInputStream)) {
  460.             in = this.in = new PushbackInputStream(in);
  461.             }
  462.             ((PushbackInputStream)in).unread(c2);
  463.         }
  464.         break loop;
  465.  
  466.           default:
  467.         if (--room < 0) {
  468.             buf = new char[offset + 128];
  469.             room = buf.length - offset - 1;
  470.             System.arraycopy(lineBuffer, 0, buf, 0, offset);
  471.             lineBuffer = buf;
  472.         }
  473.         buf[offset++] = (char) c;
  474.         break;
  475.         }
  476.     }
  477.     if ((c == -1) && (offset == 0)) {
  478.         return null;
  479.     }
  480.     return String.copyValueOf(buf, 0, offset);
  481.     }
  482.  
  483.     /**
  484.      * See the general contract of the <code>readUTF</code>
  485.      * method of <code>DataInput</code>.
  486.      * <p>
  487.      * Bytes
  488.      * for this operation are read from the contained
  489.      * input stream.
  490.      *
  491.      * @return     a Unicode string.
  492.      * @exception  EOFException  if this input stream reaches the end before
  493.      *               reading all the bytes.
  494.      * @exception  IOException   if an I/O error occurs.
  495.      * @see        java.io.DataInputStream#readUTF(java.io.DataInput)
  496.      */
  497.     public final String readUTF() throws IOException {
  498.         return readUTF(this);
  499.     }
  500.  
  501.     /**
  502.      * Reads from the
  503.      * stream <code>in</code> a representation
  504.      * of a Unicode  character string encoded in
  505.      * Java modified UTF-8 format; this string
  506.      * of characters  is then returned as a <code>String</code>.
  507.      * The details of the modified UTF-8 representation
  508.      * are  exactly the same as for the <code>readUTF</code>
  509.      * method of <code>DataInput</code>.
  510.      *
  511.      * @param      in   a data input stream.
  512.      * @return     a Unicode string.
  513.      * @exception  EOFException            if the input stream reaches the end
  514.      *               before all the bytes.
  515.      * @exception  IOException             if an I/O error occurs.
  516.      * @exception  UTFDataFormatException  if the bytes do not represent a
  517.      *               valid UTF-8 encoding of a Unicode string.
  518.      * @see        java.io.DataInputStream#readUnsignedShort()
  519.      */
  520.     public final static String readUTF(DataInput in) throws IOException {
  521.         int utflen = in.readUnsignedShort();
  522.         char str[] = new char[utflen];
  523.     int count = 0;
  524.     int strlen = 0;
  525.     while (count < utflen) {
  526.         int c = in.readUnsignedByte();
  527.         int char2, char3;
  528.         switch (c >> 4) {
  529.             case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  530.             // 0xxxxxxx
  531.             count++;
  532.             str[strlen++] = (char)c;
  533.             break;
  534.             case 12: case 13:
  535.             // 110x xxxx   10xx xxxx
  536.             count += 2;
  537.             if (count > utflen)
  538.             throw new UTFDataFormatException();
  539.             char2 = in.readUnsignedByte();
  540.             if ((char2 & 0xC0) != 0x80)
  541.             throw new UTFDataFormatException();
  542.             str[strlen++] = (char)(((c & 0x1F) << 6) | (char2 & 0x3F));
  543.             break;
  544.             case 14:
  545.             // 1110 xxxx  10xx xxxx  10xx xxxx
  546.             count += 3;
  547.             if (count > utflen)
  548.             throw new UTFDataFormatException();
  549.             char2 = in.readUnsignedByte();
  550.             char3 = in.readUnsignedByte();
  551.             if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
  552.             throw new UTFDataFormatException();
  553.             str[strlen++] = (char)(((c & 0x0F) << 12) |
  554.                        ((char2 & 0x3F) << 6) |
  555.                        ((char3 & 0x3F) << 0));
  556.             break;
  557.             default:
  558.             // 10xx xxxx,  1111 xxxx
  559.             throw new UTFDataFormatException();
  560.         }
  561.     }
  562.         return new String(str, 0, strlen);
  563.     }
  564. }
  565.